// 异步更新 DOM，防止数据更新频繁时多次重新渲染
let queue = [] // Watcher 存储队列
let has = {} // 判断 queque 是否有相同 id 的 Watcher

// 执行队列内的 watcher
function flushSchedulerQueue () {
  for (let index = 0; index < queue.length; index++) {
    let watcher = queue[index]
    let id = watcher.id
    has[id] = null
    watcher.run()
  }

  queue.length = 0
}

// 处理 queque
export default function queueWatcher (watcher) {
  const id = watcher.id
  if (has[id] == null) {
    queue.push(watcher)
  }
  setTimeout(() => flushSchedulerQueue())
}
